﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.ImportFileRequest
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Collections.Generic;
using System.IO;

namespace Microsoft.InfoCards
{
  internal class ImportFileRequest : UIAgentRequest
  {
    public ImportFileRequest(
      IntPtr rpcHandle,
      Stream inArgs,
      Stream outArgs,
      ClientUIRequest parent)
      : base(rpcHandle, inArgs, outArgs, parent)
    {
    }

    protected override void OnMarshalInArgs()
    {
    }

    protected override void OnProcess()
    {
      RoamingStoreFile context = this.ParentRequest.GetContext<RoamingStoreFile>();
      if (context == null)
        return;
      if (context.Cards == null)
        return;
      try
      {
        StoreConnection connection = StoreConnection.GetConnection();
        try
        {
          connection.BeginTransaction();
          try
          {
            foreach (InfoCard card in (IEnumerable<InfoCard>) context.Cards)
            {
              DataRow singleRow = connection.GetSingleRow(QueryDetails.FullRow, new List<QueryParameter>()
              {
                new QueryParameter("ix_objecttype", new object[1]
                {
                  (object) 1
                }),
                new QueryParameter("ix_globalid", new object[1]
                {
                  (object) GlobalId.DeriveFrom(card.Id.ToString())
                })
              }.ToArray());
              if (singleRow != null)
              {
                using (MemoryStream memoryStream = new MemoryStream(singleRow.GetDataField()))
                {
                  if (new InfoCard((Stream) memoryStream).LastUpdate < card.LastUpdate)
                  {
                    card.IsImported = true;
                    card.InstalledOn = DateTime.Now;
                    card.Save(connection);
                  }
                }
              }
              else
              {
                card.IsImported = true;
                card.InstalledOn = DateTime.Now;
                card.Save(connection);
              }
            }
            connection.CommitTransaction();
            AuditLog.AuditStoreImport();
          }
          catch (Exception ex)
          {
            connection.RollbackTransaction();
            if (!InfoCardTrace.IsFatal(ex))
              throw InfoCardTrace.ThrowHelperError((Exception) new ImportStoreException(SR.GetString("InvalidImportFile"), ex));
            throw;
          }
        }
        finally
        {
          connection.Close();
        }
      }
      finally
      {
        context.Clear();
      }
    }

    protected override void OnMarshalOutArgs()
    {
    }
  }
}
